<!--
 * @Description: 展示统计数据
 * @Author: maskMan
 * @LastEditTime: 2024-11-20 18:20:24
-->
<template>
  <div class="dashboard-editor-container">
    <!--统计-->
    <panel-group @handle-set-line-chart-data="handleSetLineChartData" />
    <!--修改用户信息-->
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="loginRules"
        :model="tempData"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="您的账号">
          <el-input v-model="tempData.account" disabled />
        </el-form-item>
        <el-form-item label="您的姓名" prop="name">
          <el-input v-model="tempData.name" placeholder="请输入您的姓名" maxlength="6" minlength="2" />
        </el-form-item>
        <el-form-item label="您的手机号" prop="phone">
          <el-input v-model="tempData.phone" placeholder="请输入您的手机号" maxlength="11" type="text" />
        </el-form-item>
        <el-form-item label="您的新密码" prop="newpassword">
          <el-input
            v-model="tempData.newpassword"
            placeholder="请输入您的新密码"
            maxlength="11"
            minlength="5"
            type="password"
          />
        </el-form-item>
        <el-form-item label="认证密码" prop="oldpassword" :label-style="{ color: 'red !important' }">
          <el-input
            v-model="tempData.oldpassword"
            placeholder="请输入您的账号密码"
            maxlength="11"
            minlength="5"
            type="password"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ '取消' }}
        </el-button>
        <el-button type="primary" @click="updateManageData()">
          {{ '确定' }}
        </el-button>
      </div>
    </el-dialog>
    <!--查看用户信息-->
    <el-dialog title="查看信息" :visible.sync="dialogInfoVisible">
      <el-form label-position="left" label-width="100px" style="width: 400px; margin-left: 50px">
        <el-form-item label="您的账号">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ tempData.account }}
          </el-tag>
        </el-form-item>
        <el-form-item label="您的身份">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ mapIndent[tempData.ident] }}
          </el-tag>
        </el-form-item>
        <el-form-item label="您的上级">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ tempData.parent }}
          </el-tag>
        </el-form-item>
        <el-form-item label="您的姓名">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ tempData.name }}
          </el-tag>
        </el-form-item>
        <el-form-item label="您的手机">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ tempData.phone }}
          </el-tag>
        </el-form-item>
        <el-form-item label="最后登录">
          <el-tag effect="light" style="width: 300px; background-color: rgba(150, 150, 150, 0.5)">
            {{ tempData.lastTime }}
          </el-tag>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--查看用户权限-->
    <el-dialog title="查看权限" :visible.sync="dialogAuthVisible">
      <tree :roleStruct="managerRoles"></tree>
    </el-dialog>
    <div class="button-container">
      <el-button type="primary" @click="dialogInfoVisible = true">查看信息</el-button>
      <el-button type="primary" @click="dialogAuthVisible = true">查看权限</el-button>
      <el-button type="primary" @click="showDialog">修改信息</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { updateManageData } from '@/api/admin';
import { ManagerModule } from '@/store/modules/manager';
import { Component, Vue } from 'vue-property-decorator';
import PanelGroup from './components/PanelGroup.vue';
//@ts-ignore
import Tree from './components/Tree.vue';
interface TempData {
  account: string;
  name: string;
  phone: string;
  oldpassword: string;
  newpassword: string;
  ident?: string;
  parent?: string;
  lastTime?: string;
}
@Component({
  name: 'DashboardAdmin',
  components: {
    PanelGroup,
    Tree
  }
})
export default class extends Vue {
  public dialogFormVisible = false;
  public dialogInfoVisible = false;
  public dialogAuthVisible = false;
  public lineChartData = [];
  public mapIndent = {
    admin: '超级管理员',
    agnet: '代理',
    customer: '客服',
    user: '用户'
  };
  public tempData: TempData = {
    account: '',
    name: '',
    phone: '',
    oldpassword: '',
    newpassword: '',
    ident: '',
    parent: '',
    lastTime: ''
  };
  public managerRoles: TRole.RoleStruct = {};
  created() {
    this.initDialog();
    this.managerRoles = ManagerModule.roles;
  }
  initDialog() {
    const temp = ManagerModule.managerData;
    this.tempData = {
      account: temp.account,
      name: temp.name,
      phone: temp.phone,
      oldpassword: '',
      newpassword: '',
      ident: temp.ident,
      parent: temp.parent,
      lastTime: temp.lastTime
    };
  }
  showDialog() {
    this.initDialog();
    this.dialogFormVisible = true;
  }
  public handleSetLineChartData(type: string) {
    this.lineChartData = [];
  }
  //修改管理信息
  public async updateManageData() {
    (this.$refs.dataForm as any).validate(async (valid: boolean) => {
      if (valid) {
        const upinfo = {};
        if (this.tempData.newpassword) {
          upinfo['newpassword'] = this.tempData.newpassword;
        }
        if (this.tempData.phone != ManagerModule.managerData.phone) {
          upinfo['phone'] = this.tempData.phone;
        }
        if (this.tempData.name != ManagerModule.managerData.name) {
          upinfo['name'] = this.tempData.name;
        }
        if (this.tempData.oldpassword) {
          upinfo['oldpassword'] = this.tempData.oldpassword;
        }
        const ret = await updateManageData(this.tempData);
        if (ret?.status) {
          const data = { ...ManagerModule.managerData };
          if (upinfo['phone']) {
            data.phone = upinfo['phone'];
          }
          if (upinfo['name']) {
            data.name = upinfo['name'];
          }
          ManagerModule.set_managerData(data);
          this.dialogFormVisible = false;
          this.$msgSuc(ret?.msg || '登录失败');
          return;
        }
        this.$msgErr(ret?.msg || '登录失败');
      } else {
        // 如果验证失败，打印错误信息
        this.$msgErr('验证失败');
        return false;
      }
    });
  }
  //验证规则
  loginRules = {
    newpassword: [
      { required: false, message: '该字段不能为空', trigger: 'blur' },
      {
        pattern: /^$|^.{5,11}$/,
        message: '密码长度必须为 5到11 个字符',
        trigger: 'blur'
      }
    ],
    oldpassword: [
      { required: true, message: '该字段不能为空', trigger: 'blur' },
      {
        pattern: /^$|^.{5,11}$/,
        message: '密码长度必须为 5到11 个字符',
        trigger: 'blur'
      }
    ],
    phone: [
      { required: false, message: '该字段不能为空', trigger: 'blur' },
      {
        pattern: /^1[3-9]\d{9}$/,
        message: '请输入有效的手机号',
        trigger: 'blur'
      }
    ],
    name: [
      { required: false, message: '该字段不能为空', trigger: 'blur' },
      {
        pattern: /^$|^.{2,6}$/,
        message: '名字长度必须为 2到6 个字符',
        trigger: 'blur'
      }
    ]
  };
}
</script>

<style lang="scss" scoped>
.button-container {
  display: flex;
  justify-content: center; /* 水平居中 */
  gap: 20px; /* 按钮之间的间距 */
}
::v-deep .el-dialog__body {
  max-height: 450px; /* 设置最大高度 */
  overflow-y: auto; /* 超过最大高度时显示滚动条 */
}
.custom-space {
  margin-right: 8px;
}
.custom-label {
  color: red !important;
}
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
